package com.example.demo;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class PrintTask {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

    @Scheduled(fixedRate = 3000)
    public void reportCurrentTime() {
        System.out.println("NOW：" + sdf.format(new Date()));
    }


    @Scheduled(cron = "0/1 * * * * ?")
    public void task01() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + " | task01 " + new Date().toLocaleString());
        Thread.sleep(2000);
    }

    @Scheduled(cron = "0/2 * * * * ?")
    public void task02() {
        System.out.println(Thread.currentThread().getName() + " | task02 " + new Date().toLocaleString());
    }
}
